#import "SEGElevatorControllSingleDoorView.h"
//图片高度
#define kImageHeight 60

//图片与文本间距
#define kImageAndLabelMargin 8

@implementation SEGElevatorControllSingleDoorViewModel
-(instancetype)init{
    if (self == [super init]) {
        _doorName = @"";
        _doorImage = @"";
        _widgetWidth = [UIScreen mainScreen].bounds.size.width;
    }
    return self;
}
@end

/**************************SEGElevatorControllSingleDoorViewLayout************************/
@implementation SEGElevatorControllSingleDoorViewLayout
- (instancetype)initWithData:(SEGElevatorControllSingleDoorViewModel *)data type:(SEGEnumElevatorControllSingleDoorViewType)type{
    if (self == [super init]) {
        _data = data;
        _type = type;
    }
    [self segElevatorControllSingleDoorViewLayout];
    return self;
}

-(void)segElevatorControllSingleDoorViewLayout{
    //高度固定
    _totalHeight = kElevatorControllSingleDoorViewHeight;
    
    //高度不固定
    //_totalHeight = 0;
    _doorNameLabelHeight = 0;
    _doorNameLabelTextLayout = nil;
    
    if (!_data) return;
    
    [self layout];
}

//数据处理
-(void)layout{
    [self segdoorNameLabelWithContent:_data.doorName fontSize:14 textColor:kColor_1];
}
//控件doorNameLabel文本布局计算
-(void)segdoorNameLabelWithContent:(NSString*)content fontSize:(CGFloat)fontSize textColor:(UIColor*)color{
    if(!content)return;
    UIFont *font = [UIFont fontWithName:@"Heiti SC" size:fontSize];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:content];
    text.yy_font = font;
    text.yy_color = color;
    
    SEGContentTextLinePositionModifier *modifier = [SEGContentTextLinePositionModifier new];
    modifier.font = font;
    modifier.paddingTop = 1;
    modifier.paddingBottom = 1;
    //modifier.lineHeightMultiple = 1;
    
    SEGTextContainer *container = [SEGTextContainer new];
    container.size = CGSizeMake(_data.widgetWidth - 30, HUGE);
    container.linePositionModifier = modifier;
    container.maximumNumberOfRows = 2;
    
    _doorNameLabelTextLayout = [SEGTextLayout layoutWithContainer:container text:text];
    if (!_doorNameLabelTextLayout)return;
    _doorNameLabelHeight = [modifier heightForLineCount:_doorNameLabelTextLayout.rowCount];
    _doorNameLabelHeight += 10;
}

@end

/**************************SEGElevatorControllSingleDoorView************************/
@implementation SEGElevatorControllSingleDoorView
- (instancetype)initWithFrame:(CGRect)frame {
    if (frame.size.width == 0 && frame.size.height == 0) {
        frame.size.width = [UIScreen mainScreen].bounds.size.width;
        frame.size.height = kElevatorControllSingleDoorViewHeight;
    }
    self = [super initWithFrame:frame];
    
    [self addSubview:self.doorNameLabel];
    
    [self addSubview:self.doorImageView];
    
    if (kDebug)self.backgroundColor = kSegRandomColor;
    return self;
}

-(void)setWithLayout:(SEGElevatorControllSingleDoorViewLayout *)layout{
    self.height = layout.totalHeight;
    self.width = layout.data.widgetWidth;
    _layout = layout;
    
    if (self.height <= 0) return;
    
    self.doorNameLabel.textLayout = layout.doorNameLabelTextLayout;
    [self.doorImageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kClientImageRootURL,@""]] placeholderImage:[UIImage imageWithContentsOfFile:SEGPicName(@"pic_default_300_300")]];
    
    [self setupUI];
}

//界面布局
-(void)setupUI{
    [self segNormal];
    
    self.doorNameLabel.textAlignment = NSTextAlignmentCenter;
    self.doorNameLabel.textVerticalAlignment = SEGTextVerticalAlignmentTop;
}

-(void)segNormal{
    [self.doorImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self);
        make.width.height.mas_equalTo(kImageHeight);
    }];

    [self.doorNameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).mas_offset(15);
        make.top.equalTo(self.doorImageView.mas_bottom).mas_offset(kImageAndLabelMargin);
        make.height.mas_equalTo(_layout.doorNameLabelHeight);
        make.width.mas_equalTo(_layout.data.widgetWidth - 30);
    }];
    
}

#pragma mark - getter
-(SEGLabel *)doorNameLabel{
    if (!_doorNameLabel) {
        _doorNameLabel = [[SEGLabel alloc]init];
        _doorNameLabel.userInteractionEnabled = NO;
        _doorNameLabel.textAlignment = NSTextAlignmentCenter;
        _doorNameLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
        _doorNameLabel.displaysAsynchronously = YES;
        //_doorNameLabel.ignoreCommonProperties = YES;//YES会影响系统属性
        _doorNameLabel.fadeOnHighlight = NO;
        _doorNameLabel.numberOfLines = 0;
        _doorNameLabel.fadeOnAsynchronouslyDisplay = NO;
        _doorNameLabel.fadeOnAsynchronouslyDisplay = NO;
        _doorNameLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        if (kDebug)_doorNameLabel.backgroundColor = kSegRandomColor;
        //__weak typeof(self) _self = self;
        //_doorNameLabel.textTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        //[SEGAlertView showTmepMessageWithMessage:@"_doorNameLabelClick"];
        //if (_self.delegate && [_self.delegate respondsToSelector:@selector(SEGprotocoldoorNameLabel:)]) {
        //[_self.delegate SEGprotocoldoorNameLabel:_self.doorNameLabel];
        //}
        //};
    }
    return _doorNameLabel;
}


-(UIImageView *)doorImageView{
    if (!_doorImageView) {
        _doorImageView = [[UIImageView alloc]init];
        _doorImageView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"")];
        _doorImageView.contentMode = UIViewContentModeScaleAspectFill;
        _doorImageView.layer.masksToBounds = YES;
        _doorImageView.layer.cornerRadius = kImageHeight * 0.5;
        if (kDebug)_doorImageView.backgroundColor = kSegRandomColor;
    }
    return _doorImageView;
}


@end

